﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using EMoney.IStock.Client.Models.QQ;

namespace EMoney.IStock.Client.DataAccess
{
    public class FocusStockDal
    {
        /// <summary>
        /// 批量更新交易数据
        /// </summary>
        /// <param name="dt"></param>
        public void AddDealOrders(DataTable dt)
        {
            SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(ConnectionManager.EMoney_IStock_JRPT, SqlBulkCopyOptions.UseInternalTransaction);
            sqlBulkCopy.DestinationTableName = "QQ_DealOrders";
            foreach (DataColumn c in dt.Columns)
            {
                sqlBulkCopy.ColumnMappings.Add(c.ColumnName, c.ColumnName);
            }
            sqlBulkCopy.BulkCopyTimeout = 1800;  //超时时间 
            sqlBulkCopy.BatchSize = 3000;  //每次传输3000行 
            sqlBulkCopy.WriteToServer(dt);
        }

        /// <summary>
        /// 更新聚焦数据，同时删除当天小于传入时间的数据
        /// </summary>
        /// <param name="lastDate">上一次最大更新ID</param>
        /// <returns></returns>
        public int AddFocusStock(long lastID)
        {
            SqlParameter[] values = { 
                                        new SqlParameter("@LastID", lastID)
                                    };
            return SQLHelper.ExecuteNonQuery(ConnectionManager.EMoney_IStock_JRPT, CommandType.StoredProcedure, "SP_AddFocusStockList", values);
        }

        /// <summary>
        /// 根据开始日期获取最大入库ID
        /// </summary>
        /// <param name="lastID">上一次最大ID</param>
        /// <returns></returns>
        public long GetDealOrdersMaxID(long lastID)
        {
            SqlParameter[] values = { 
                                        new SqlParameter("@LastID", lastID)
                                    };
            object o = SQLHelper.ExecuteScalar(ConnectionManager.EMoney_IStock_JRPT, CommandType.StoredProcedure, "SP_GetDealOrdersMaxID", values);
            return Convert.ToInt64(o);
        }

        /// <summary>
        /// 根据开始日期获取最近聚焦个股数据
        /// </summary>
        /// <param name="startDate">开始时间</param>
        /// <returns></returns>
        public List<TB_FocusStock> GetFocusStockList(DateTime startDate, DateTime endDate)
        {
            SqlParameter[] values = {
                                        new SqlParameter("@StartTime", startDate.Date),
                                        new SqlParameter("@EndTime",endDate.Date)
                                    };
            return SQLHelper.GetProcedureDataByColumnsLst<TB_FocusStock>(ConnectionManager.EMoney_IStock_JRPT, "SP_GetFocusStockList", values);
        }

        /// <summary>
        /// 根据开始日期获取最近聚焦板块数据
        /// </summary>
        /// <param name="startDate">开始时间</param>
        /// <returns></returns>
        public List<TB_FocusBlock> GetFocusBlockList(DateTime startDate, DateTime endDate)
        {
            SqlParameter[] values = {
                                        new SqlParameter("@StartTime", startDate.Date),
                                        new SqlParameter("@EndTime",endDate.Date)
                                    };
            return SQLHelper.GetProcedureDataByColumnsLst<TB_FocusBlock>(ConnectionManager.EMoney_IStock_JRPT, "SP_GetFocusBlockList", values);
        }

        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public TB_JRPT_UserInfo GetUserInfo(string uid)
        {
            string sql = "SELECT * FROM TB_JRPT_UserInfo WITH(NOLOCK) WHERE UserID=@UID";
            SqlParameter[] values = {
                                        new SqlParameter("@UID",uid)
                                    };
            return SQLHelper.GetDataByColumns<TB_JRPT_UserInfo>(ConnectionManager.EMoney_IStock_JRPT, sql, values);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddUserInfo(TB_JRPT_UserInfo user, ref string msg)
        {
            SQLTools tools = new SQLTools();
            tools.AddSqlParameter("@ID", SqlDbType.BigInt, user.ID);
            tools.AddSqlParameter("@UserID", SqlDbType.NVarChar, 50, user.UserID);
            tools.AddSqlParameter("@EMName", SqlDbType.NVarChar, 50, user.EMName);
            tools.AddSqlParameter("@PID", SqlDbType.NVarChar, 50, user.PID);
            tools.AddSqlParameter("@QQ", SqlDbType.NVarChar, 50, user.QQ);
            tools.AddSqlParameter("@OpenID", SqlDbType.NVarChar, 50, user.OpenID);
            tools.AddSqlParameter("@ReturnMsg", SqlDbType.NVarChar, 50, msg, ParameterDirection.InputOutput);
            int rows = SQLHelper.ExecuteNonQuery(ConnectionManager.EMoney_IStock_JRPT, CommandType.StoredProcedure, "SP_AddJRPTUserInfo", tools.GetSqlParameters());
            msg = tools.GetSqlParameterByName("ReturnMsg").Value.ToString();
            return rows;
        }
    }
}
